package merisis.util;


public class EncodeUtil {

	public final static char[] codetable = {

	'0', '1', '2', '3', '4', '5', '6', '7',

	'8', '9', 'A', 'B', 'C', 'D', 'E', 'F',

	'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N',

	'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', };

	public static String encodeBase32(long no, int len) {

		StringBuilder sb = new StringBuilder();
		sb.setLength(len);

		int pos = len - 1;
		do {
			sb.setCharAt(pos--, codetable[(int) (no & 0x1F)]);
			no >>>= 5;
			if (pos < 0)
				throw new IllegalStateException(
						"overflow in base32 encoding, maxlength =" + len
								+ ", num=" + no);
		} while (no > 0);

		while (pos >= 0)
			sb.setCharAt(pos--, '0');

		return sb.toString();
	}
}
